import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import { regMsgEvent, EVENT_TYPE } from '@utility/eventBus.js'
import { debounce, parseJsonStr } from '@utility/utility'
import { ROUTER_GROUPS } from '@utility/constant.js'

Vue.use(VueRouter)

const perPages = [
  'JXGL_OTHER_MANAGE',
  'JXGL_OTHER_PLAT_MANAGE',
  'JXGL_OTHER_HELP_MANAGE',
  'JXGL_MOULD_MANAGE',
  'JXGL_ASSESS_MANAGE',
  'JXGL_ORGAN_MANAGE',
  'JXGL_CONSUMER_MANAGE',
  'JXGL_ROLE_MANAGE',
  'JXGL_POST_MANAGE',
  'JXGL_OTHER_DOC_MANAGE',
  'JXKH_SYSTEM',
  'JXKH_DELAY_MANAGE',
  'homePage',
]

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return routerPush.call(this, location, onResolve, onReject)
  return routerPush.call(this, location).catch(error => error)
}
// import pdf from '@views/home/pdf.vue'

/**
 * meta:属性说明
 * title：面包屑显示内容
 * breadcrumbHide：是否不添加到面包屑中， 如果设置为true， 则不会在breadcrumb面包屑中显示(默认 false)
 * keepAlive：是否缓存
 * deepth：页面层级，用于是否更新缓存，由大到小不更新，小到大更新，
 *
 */

const routes = [
  {
    // main layout 全屏根路由
    path: '/',
    name: 'mainLayout',
    component: () => import('@layouts/main.vue'),
    redirect: '/login',
    children: [
      {
        // header layout 提供 通用 header 组件
        path: '/layout-header',
        name: 'layoutHeader',
        component: () => import('@layouts/header.vue'),
        children: [
          // 首页
          {
            path: '/home',
            name: 'homePage',
            component: () => import('@views/homePage/index.vue'),
            meta: {
              title: '首页',
              deepth: 1,
              group: ROUTER_GROUPS.home,
            },
          },
          {
            // slide layout 提供 slide 导航
            path: '/layout-slide',
            name: 'layoutSlide',
            component: () => import('@layouts/slide.vue'),
            children: [
              {
                // sub layout 提供 面包屑导航
                path: '/layout-sub',
                name: 'layoutSub',
                component: () => import('@layouts/sub.vue'),
                children: [
                  {
                    path: '/mould',
                    name: 'JXGL_MOULD_MANAGE',
                    component: () => import('@views/mould/index.vue'),
                    meta: {
                      title: '考核模板',
                      deepth: 1,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/mould/addedit/:id?',
                    name: 'JXGL_MOULD_MANAGE_ADDEDIT',
                    component: () => import('@views/mould/addEdit.vue'),
                    meta: {
                      title: '考核模板添加编辑',
                      deepth: 2,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/depart',
                    name: 'JXGL_DEPART_MANAGE',
                    component: () => import('@views/depart/index.vue'),
                    meta: {
                      title: '部门管理',
                      deepth: 2,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/consumer',
                    name: 'JXGL_CONSUMER_MANAGE',
                    component: () => import('@views/consumer/index.vue'),
                    meta: {
                      title: '用户管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/role',
                    name: 'JXGL_ROLE_MANAGE',
                    component: () => import('@views/role/index.vue'),
                    meta: {
                      title: '角色管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/other/document',
                    name: 'JXGL_OTHER_DOC_MANAGE',
                    component: () => import('@views/other/document/index.vue'),
                    meta: {
                      title: '纪实管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/other/plat',
                    name: 'JXGL_OTHER_PLAT_MANAGE',
                    component: () => import('@views/other/set/index.vue'),
                    meta: {
                      title: '平台设置',
                      deepth: 1,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/other/help',
                    name: 'JXGL_OTHER_HELP_MANAGE',
                    component: () => import('@views/other/list/index.vue'),
                    meta: {
                      title: '考核帮助',
                      deepth: 1,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/assess',
                    name: 'JXGL_ASSESS_MANAGE',
                    component: () => import('@views/assess/index.vue'),
                    meta: {
                      title: '考核方案管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/assess/detail',
                    name: 'JXGL_ASSESS_DETAIL',
                    component: () => import('@views/assess/assessDetail.vue'),
                    meta: {
                      title: '考核方案管理',
                      deepth: 2,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/idx/lib',
                    name: 'JXGL_INDEX_MANAGE',
                    component: () => import('@views/index/index.vue'),
                    meta: {
                      title: '指标模型',
                      deepth: 2,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/idx/detail/:id?',
                    name: 'JXGL_INDEX_DETAIL',
                    component: () => import('@views/index/detail.vue'),
                    meta: {
                      title: '指标模型',
                      deepth: 2,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/post',
                    name: 'JXGL_POST_MANAGE',
                    component: () => import('@views/post/index.vue'),
                    meta: {
                      title: '职位管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.jxgl,
                    },
                  },
                  {
                    path: '/document/index-list',
                    name: 'PSKH_DOCUMENTARY_EDITION',
                    component: () => import('@views/document/indexList.vue'),
                    meta: {
                      title: '纪实本',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/document/index-write',
                    name: 'PSKH_DOCUMENTARY_EDITION_WRITE',
                    component: () => import('@views/document/indexWrite.vue'),
                    meta: {
                      title: '纪实本',
                      deepth: 2,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/document/index-detail',
                    name: 'PSKH_DOCUMENTARY_EDITION_DETAIL',
                    component: () => import('@views/document/indexDetail.vue'),
                    meta: {
                      title: '纪实本',
                      deepth: 3,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/evaluate/list',
                    name: 'PSKH_EVALUATION_COLUMN',
                    component: () => import('@views/evaluate/list/index.vue'),
                    meta: {
                      title: '评议栏',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/evaluate/edit',
                    name: 'PSKH_EVALUATION_COLUMN',
                    component: () => import('@views/evaluate/edit/index.vue'),
                    meta: {
                      title: '评议栏',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/grade-manage',
                    name: 'PSKH_GRADE_MANAGE',
                    component: () => import('@views/grade/index.vue'),
                    meta: {
                      title: '等次管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/examine/file/gztz',
                    name: 'PSKH_WORK_NOTICE',
                    component: () => import('@views/articles/list/index.vue'),
                    meta: {
                      title: '工作通知',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/examine/file/zdwj',
                    name: 'PSKH_SYSTEM_FILE',
                    component: () => import('@views/articles/list/index.vue'),
                    meta: {
                      title: '制度文件',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/examine/file/add/:type?',
                    name: 'PSKH_SYSTEM_FILE_ADD',
                    component: () => import('@views/articles/list/fileAdd.vue'),
                    meta: {
                      title: '制度文件',
                      deepth: 2,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/examine/file/detail/:id?/:type?',
                    name: 'PSKH_SYSTEM_FILE_DETAIL',
                    component: () => import('@views/articles/list/fileDetail.vue'),
                    meta: {
                      title: '通知详情',
                      deepth: 3,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/roster/honor',
                    name: 'PSKH_HONOR_ROSTER',
                    component: () => import('@views/roster/honor/index.vue'),
                    meta: {
                      title: '公示榜',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/system/message',
                    name: 'PSKH_SYSTEM_MESSAGE',
                    component: () => import('@views/system/message/index.vue'),
                    meta: {
                      title: '系统消息',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/bonus/add',
                    name: 'PSKH_BONUS_ADD',
                    component: () => import('@views/bonus/index.vue'),
                    meta: {
                      title: '加分项',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/minzhu/create',
                    name: 'PSKH_MINZHU_ASSESS',
                    component: () => import('@views/minZhu/create/index.vue'),
                    meta: {
                      title: '民主测评',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/minzhu/person',
                    name: 'KH_MINZHU_PERSON',
                    component: () => import('@views/minZhu/person/index.vue'),
                    meta: {
                      title: '人员管理',
                      deepth: 2,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/minzhu/assess',
                    name: 'PSKH_MINZHU_MANAGE_ASSESS',
                    component: () => import('@views/minZhu/assess/index.vue'),
                    meta: {
                      title: '测评管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/attendance',
                    name: 'PSKH_ATTENDANCE_MANAGE',
                    component: () => import('@views/attendance/index.vue'),
                    meta: {
                      title: '日常考勤',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/attendance/check',
                    name: 'PSKH_ATTENDANCE_MANAGE',
                    component: () => import('@views/attendance/indexCheck.vue'),
                    meta: {
                      title: '日常考勤',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/portrait',
                    name: 'PSKH_JXXIAO_PORTRAIT',
                    component: () => import('@views/portrait/index.vue'),
                    meta: {
                      title: '绩效画像',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                      keepAlive: true,
                    },
                  },
                  {
                    path: '/portrait/contrast',
                    name: 'PSKH_JXXIAO_PORTRAIT_CONTRAST',
                    component: () => import('@views/portrait/Self/PersonContrast.vue'),
                    meta: {
                      title: '绩效画像',
                      deepth: 2,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/delay',
                    name: 'JXKH_DELAY_MANAGE',
                    component: () => import('@views/delay/index.vue'),
                    meta: {
                      title: '延期管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.system,
                    },
                  },
                  {
                    path: '/year',
                    name: 'PSKH_YEAR_EXAMINE',
                    component: () => import('@views/year/index.vue'),
                    meta: {
                      title: '年度考核',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/meet',
                    name: 'PSKH_MEETING_MANAGE',
                    component: () => import('@views/meet/index.vue'),
                    meta: {
                      title: '会议管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/daily',
                    name: 'PSKH_DAILY_MANAGE',
                    component: () => import('@views/daily/index.vue'),
                    meta: {
                      title: '日常管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                  {
                    path: '/daily/check',
                    name: 'PSKH_DAILY_MANAGE_CHECK',
                    component: () => import('@views/daily/indexCheck.vue'),
                    meta: {
                      title: '日常管理',
                      deepth: 1,
                      group: ROUTER_GROUPS.pskh,
                    },
                  },
                ],
              },
            ],
          },

          // 无权限
          {
            path: '/403',
            name: 'NO_ACCESS',
            component: () => import('@views/auth/403.vue'),
          },
          { path: '/404', name: 'pageNotFound', component: () => import('@views/auth/404.vue') },
        ],
      },
      {
        path: '/assess/page/:id',
        name: 'JXGL_ASSESS_PAGE',
        component: () => import('@views/assessPage/index.vue'),
        meta: {
          title: '考核详情',
          deepth: 2,
        },
      },
      {
        path: '/pdf',
        name: 'PDF_PAGE',
        component: () => import('@views/pdfViewer/index.vue'),
      },
      {
        path: '/',
        redirect: '/login',
      },
      {
        path: '/login',
        name: 'LOGIN',
        component: () => import('@views/auth/Login.vue'),
      },
    ],
  },
  // 帮助文档、隐私条款、服务协议
  {
    path: '/privacy',
    name: 'privacyPolicy',
    component: () => import('@views/help/privacy.vue'),
    meta: {
      title: '隐私条款',
      deepth: 0,
    },
  },
  {
    path: '/service',
    name: 'serviceAgreement',
    component: () => import('@views/help/service.vue'),
    meta: {
      title: '服务协议',
      deepth: 0,
    },
  },
  { path: '*', redirect: '/404' },
]

const router = new VueRouter({
  // base: import.meta.env.BASE_URL,
  routes,
})

router.beforeEach((to, from, next) => {
  // 规避重复push路由问题，会引起replace自身时不起作用
  if (to.fullPath === from.fullPath) {
    return false
  } else {
    if (perPages.includes(to.name)) {
      // 判断用户是否有权限进入该页面
      const codesStr = localStorage.getItem('PERMISSION_CODES')
      if (codesStr) {
        const menuCodes = parseJsonStr(codesStr)
        if (menuCodes && menuCodes.includes(to.name)) {
          next()
        } else {
          next({ path: '/403' })
        }
      } else {
        next()
      }
    } else {
      next()
    }
  }
})

router.afterEach((to, from) => {
  const cachedPages = store.getters.cachedPages
  const routerPages = JSON.parse(sessionStorage.getItem('routerPages')) || []
  if (routerPages.length > 0 && !routerPages.includes(to.name)) {
    routerPages.push(to.name)
    sessionStorage.setItem('routerPages', JSON.stringify(routerPages))
  }
  // home 页面 置空缓存页面
  if (to.name === 'home') {
    store.commit('setCachedPages', [])
    return
  }
  if (to.meta.keepAlive) {
    if (!cachedPages.includes(to.name)) {
      cachedPages.push(to.name)
      store.commit('setCachedPages', cachedPages)
    }
  }
  // 如果 要 form(离开) 的页面是 keepAlive缓存的，
  // 再根据 deepth 来判断是前进还是后退
  // 如果是后退
  if (from && cachedPages.includes(from.name)) {
    if (!from.meta.keepAlive || to.meta.deepth < from.meta.deepth) {
      const fromPageIdx = cachedPages.indexOf(from.name)
      cachedPages.splice(fromPageIdx, 1)
      store.commit('setCachedPages', cachedPages)
    }
  }
})

regMsgEvent(
  EVENT_TYPE.goLogin,
  debounce(() => {
    // 规避短时间内重复调用问题
    router.replace({
      name: 'LOGIN',
    })
  }, 2000)
)

export default router
